import sys
import os
root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root_path)
from glob import glob
import os
import pandas as pd
import numpy as np

from sample.config.global_data import change_list, test_data_path, train_data_path


def add_header(file):
    try:
        csv = pd.read_csv(file, header=None)
        data = csv.values
        ts = data[:, 0].astype(int)
        ts = ts - ts % 60
        val = data[:, 1]
        labels = np.zeros_like(ts)
        df = pd.DataFrame({
            'timestamp': ts,
            'value': val,
            'label': labels
        })
        df = df.drop_duplicates(subset=['timestamp'])
        df.to_csv(file, index=False)
    except:
        print(f'{file}已经预处理过了')


if __name__ == '__main__':
    train_file_list = glob(os.path.join(train_data_path, '**', '*.csv'), recursive=True)
    # for train_file in train_file_list:
    #     add_header(train_file)
    for change in change_list:
        test_file_list = glob(os.path.join(test_data_path, change['name'], '**', '*.csv'), recursive=True)
        for test_file in test_file_list:
            add_header(test_file)
